var favoriteXML = null;

/**
 * .disableTextSelect - Disable Text Select Plugin
 *
 * Version: 1.0
 * Updated: 2007-08-11
 *
 * Used to stop users from selecting text
 *
 * Copyright (c) 2007 James Dempster (letssurf@gmail.com, http://www.jdempster.com/category/jquery/disabletextselect/)
 *
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 **/

/**
 * Requirements:
 * - jQuery (John Resig, http://www.jquery.com/)
 **/
(function($) {
    if ($.browser.mozilla) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : 'none'
                });
            });
        };
    } else if ($.browser.msie) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).bind('selectstart', function() {
                    return false;
                });
            });
        };
    } else {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).mousedown(function() {
                    return false;
                });
            });
        };
    }
})(jQuery);

// End Disable Text Select Plugin

$.fn.tagName = function() {
    return this.get(0).tagName;
}

$(document).ready(function() {
	$('#favorite_link').bind('click', favorite);
	$('#favorite_items').load(baseURI+'/admin/cp/ajax_load_favorites').show();
});

function isMouseOn (element, parentId) {
	if (element.tagName() == 'BODY') {
		return false;
	} else if ($(element).attr('id') == parentId) {
		return true;
	} else {
		return isMouseOn (element.parent(), parentId);
	}
}


function favorite() {
	if (arguments.length == 1 && arguments[0] === true) {
		var activeDropGetter = true;
	} else {
		var activeDropGetter = false;
	}
	
	$('#favorite_link').blur();
	var oDialog = $('#favorite');
	
	if (activeDropGetter) {
		$('#item_getter').show();
		$('#empty_favorite').hide();
	}
	
	oDialog.css({
		left: $('#favorite_link').offset().left+'px',
		top: ($('#favorite_link').height()+$('#favorite_link').offset().top+3)+'px'
	});
	
	oDialog.show();
	
	$(document).bind('click',checkMouseOnFavorute);
		
	return false;
}

function checkMouseOnFavorute(e) {
	if (!isMouseOn($(e.target), 'favorite')) {
		hideFavorite();
		$(document).unbind('click',checkMouseOnFavorute);
	}
}

function hideFavorite() {
	$('#item_getter').hide();
	$('#favorite').hide();
}




function dragItemToFavorite(elementId, cpItemId) {
	$('#'+elementId).disableTextSelect();
	
	var element = $('#'+elementId);
	var elementClone = element.clone().attr('id', 'favorite_'+cpItemId);
		
	elementClone
		.css({
			position: 'absolute',
			zIndex: 3,
			top: element.offset().top + 'px',
			left: element.offset().left + 'px',
			backgroundColor: '#fff'
		});

	$('#drag_wrapper_'+cpItemId).append(elementClone);
	element.animate({opacity: '0.5'}, 'fast');
	
	var data = {
		'element': elementClone,
		'elementId': elementId,
		'cpItemId': cpItemId,
		wasTop: elementClone.offset().top,
		wasLeft: elementClone.offset().left
	};
	
	$(document)
		.bind('mousemove', data, moveItem)
		.bind('mouseup', data, returnDrag);
	
	favorite(true);
}

function moveItem(e) {
	$(e.data.element).css({
		top: (e.pageY+10)+'px',
		left: (e.pageX+6)+'px'
	});
	
	$(document.body).disableTextSelect();
	$('.favorite_dialog').disableTextSelect();
	
	if ($(e.target).attr('id') == 'item_getter') {
		$('#item_getter').attr('class','getter_enabled');
	} else {
		$('#item_getter').attr('class','getter_disabled');
	}
}

function returnDrag(e) {	
	if ($(e.target).attr('id') == 'item_getter') {
		var newDivItem = $('<div></div>').attr('class','item');
		
		$('#favorite_items').append(newDivItem.append(e.data.element)).show();
		
		$(e.data.element)
			.css({
				position: 'static',
				top: null,
				left: null,
				backgroundColor: null
			});
		
		$('#item_getter').hide();
		$('#empty_favorite').remove();
		
		ajax_add_favorite(e.data.cpItemId);
	} else {
		$(e.data.element).remove();
		hideFavorite();
	}
		
	$('#'+e.data.elementId).animate({opacity: 1}, 'fast');
	
	$(document)
		.unbind('mousemove', moveItem)
		.unbind('mouseup', returnDrag);
}

function ajax_add_favorite(id) {
	$.ajax({
		url: baseURI+'/admin/cp/ajax_add_favorite/'+id+'?scode='+scode,
		dataType: 'xml',
		type: 'get',
		cache: false,
		error: function() { alert ('Ошибка в процессе добавление избранного!')  },
		success: function(xml) {}
	});
}

function deleteFavorite(id, elementId) {
	$('#'+elementId).remove();
	
	$.ajax({
		url: baseURI+'/admin/cp/ajax_delete_favorite/'+id+'?scode='+scode,
		dataType: 'xml',
		type: 'get',
		cache: false,
		error: function() { alert ('Ошибка в процессе удаления!')  },
		success: function(xml) {}
	});
}